<template>
  <div class="ChildPage TablePage" v-loading="loading">
    <vb-page ref="page" @onTableSelection="doSelect">
      <template #headerSearch-btn-append="{ searchForm }">
        <el-button type="warning" size="small" icon="el-icon-download" @click="doDownload(searchForm)">导出生产任务审核报表</el-button>
      </template>
      <template #table-handle-prepend="scope">

        <el-button size="mini" type="text" icon="el-icon-edit" @click="gotoDetail(scope.row.billNo)">查看</el-button>
<!--        <el-button size="mini" type="text" style="color:var(&#45;&#45;el-color-success)" icon="el-icon-share" @click="toExport(scope.row.billNo)">导出</el-button>-->
      </template>
      <template #table-stockNo="scope">
        {{ getStockName(scope.row.stockNo) }}
      </template>
      <template #table-count="scope">
        {{ scope.row.count? Number(scope.row.count.toFixed(2)): scope.row.count }}
      </template>
      <template #table-pickCount="scope">
        {{ scope.row.pickCount? Number(scope.row.pickCount.toFixed(2)): scope.row.pickCount }}
      </template>
      <template #table-returnCount="scope">
        {{ scope.row.returnCount? Number(scope.row.returnCount.toFixed(2)): scope.row.returnCount }}
      </template>
      <template #table-desCount="scope">
        {{ scope.row.desCount? Number(scope.row.desCount.toFixed(2)): scope.row.desCount }}
      </template>
      <template #table-taskCount="scope">
        {{ scope.row.taskCount? Number(scope.row.taskCount.toFixed(2)): scope.row.taskCount }}
      </template>
      <template #table-savePrdCount="scope">
        {{ scope.row.savePrdCount? Number(scope.row.savePrdCount.toFixed(2)): scope.row.savePrdCount }}
      </template>
      <template #table-colDesCount="scope">
        {{ scope.row.colDesCount? Number(scope.row.colDesCount.toFixed(2)): scope.row.colDesCount }}
      </template>
      <template #table-colReturnCount="scope">
        {{ scope.row.colReturnCount? Number(scope.row.colReturnCount.toFixed(2)): scope.row.colReturnCount }}
      </template>
      <template #table-supCount="scope">
        {{ scope.row.supCount? Number(scope.row.supCount.toFixed(2)): scope.row.supCount }}
      </template>
      <template #table-status="scope">
        <el-tag :type="scope.row.status === 'save'? 'info': (scope.row.status === 'audit'? 'success': 'danger')">{{ scope.row.status === 'save'? '保存': (scope.row.status === 'audit'? '审核': '关闭') }}</el-tag>
      </template>
      <template #footerBtn="data">
        已选行数:{{ selectionLength}}
        <el-button type="primary" @click="doSelectClose" icon="el-icon-s-home" size="mini">选择行关闭</el-button>
      </template>
    </vb-page>

  </div>
</template>

<script>
export default {
  data() {
    return {
      materialLoading: false,
      reloadMaterial: false,
      selection: null,
      selectionLength:0
    }
  },
  async mounted() {
    await this.getStock()
  },
  methods: {
    async doDownload(searchForm) {
      this.loading = true
      try {
        let res = await this.callApi('download_prd_task_audit_list', searchForm)
        if (!res.res) return
      } finally {
        this.loading = false
      }
    },
    async getStock() {
      this.loading = true;
      try {
        let res = await this.$api.getChildDictionaryListByKeys({
          keys: ['stock']
        });
        if (!res.res) return;
        this.stockList = res.stock;
      } finally {
        this.loading = false;
      }
    },
    /**
     * 获取状态名
     */
    getStockName(val) {
      if (this.stockList && this.stockList.length > 0) {
        for(let i = 0; i < this.stockList.length; i++) {
          if (this.stockList[i].key === val) {
            return this.stockList[i].name;
          }
        }
      }
      return '';
    },
    async doSelectClose() {
      if (!this.selection || this.selection.length === 0) {
        this.$message.error('没有选择行')
        return
      }
      let billNoList = []
      for(let i = 0; i < this.selection.length; i++) {
        billNoList.push(this.selection[i].billNo)
      }
      try {
        await this.$confirm(`确定关闭单据吗`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
      } catch(e) {
        return
      }
      this.loading = true
      try {
        let res = await this.callApi('close_prd_order', { billNoList: billNoList })
        if (!res.res) {
          return
        }
        this.$message.success(`批量关闭完成!`)
        this.$refs.page.reload()
      } finally {
        this.loading = false
      }
    },
    doSelect(data) {
      this.selection = data
      this.selectionLength = this.selection.length;
      // console.log(data)
      // console.log(data[0].billNo)
    },
    gotoDetail(billNo = '') {
      let url = '/produce/produceTaskAuditDetail'
      if (billNo) {
        url += `?billNo=${ billNo }`
      }
      this.$router.push(url)
    }
  }
}
</script>

<style scoped>

</style>
